import { ApiCall } from "tsrpc";
import { ReqCreateRoom, ResCreateRoom } from "../../../tsrpc/shared/protocols/hall/PtlCreateRoom";

import { hs } from "../../../HallServer";
import { ecs } from "../../../core/ecs/ECS";
import { Room } from "../../../module/room/Room";
import { RoomState } from "../../../tsrpc/shared/type/RoomInfo";
import { account } from "../../../module/account/Account";
import { Config, randomRoomServerUrl } from "../../../module/config/Config";
import { ShareConfig } from "../../../module/config/ShareConfig";

export default async function (call: ApiCall<ReqCreateRoom, ResCreateRoom>) {
    //登录令牌不存在
    if (!call.req.__ssoToken) {
        call.error("登录令牌不存在", { code: 'SSO_NoNull' });
        return;
    }
    // TODO
    const roomName = call.req.roomName;
    if (roomName.length == 0) {
        call.error(`房间名不能为空`, { code: 'RoomName_NoNull' });
        return;
    }
    //判断有没有重名的房间
    // if (hs.HallModel.rooms.has(roomName)) {
    //     call.error(`房间名重复`, { code: 'RoomName_Repeat' });
    //     return;
    // }

    var dUser = account.parseSSO(call.req.__ssoToken);
    if (!dUser) {
        call.error(`登录令牌无效`, { code: 'SSO_Invalid' });
        return;
    }

    //先在这里创建房间
    // const entity = ecs.getEntity(Room);
    // hs.HallModel.rooms.set(entity.RoomModel.roomId, entity);
    // entity.RoomModel.roomName = roomName;

    const roomInfo = await hs.HallModel.hsRoom.callApi("admin/ProductRoom", {roomName});
    if (roomInfo.isSucc) {
        call.succ({ roomInfo: roomInfo.res.roomInfo });
    } else {
        call.error(`无法创建房间`, { code: 'CREATE_FAILED' });
    }

    
    
}